package dhz.tools.service;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;

import org.apache.log4j.Logger;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;

import com.yahoo.platform.yui.compressor.CssCompressor;
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;

import dhz.tools.dialog.JscssDialog;
import dhz.tools.util.IOUtils;

/**
 * JS、CSS压缩
 * @author dhz
 */
public class CompressorService {

	private static final Logger LOG = Logger.getLogger(CompressorService.class);
	
	private int linebreak = -1;
	private boolean munge = true;
	private boolean verbose = false;
	private boolean preserveAllSemiColons = false;
	private boolean disableOptimizations = false;
	
	/**
	 * JS压缩
	 * @param shell
	 * @param src	待压缩文件
	 * @param dest	压缩后输出的目标文件
	 * @throws Exception
	 */
	public void compressorJs(final Shell shell,File src,File dest) throws Exception{
		Reader reader = null;
		Writer writer = null;
		try {
			reader = new FileReader(src);
			writer = new FileWriter(dest);
			JavaScriptCompressor jsCompressor = new JavaScriptCompressor(reader,new ErrorReporter(){
				public void error(String message,String sourceName,int line,String lineSource,int lineOffset) {
					MessageDialog.openError(shell,"错误",line + ':' + lineOffset + ':' + message);
				}
				public EvaluatorException runtimeError(String message,String sourceName,int line,String lineSource,int lineOffset) {
					error(message, sourceName, line, lineSource, lineOffset);
			        return new EvaluatorException(message);
				}
				public void warning(String message,String sourceName,int line,String lineSource,int lineOffset) {
					LOG.debug("[WARNING] " + line + ':' + lineOffset + ':' + message);
				}
			});
			jsCompressor.compress(writer,linebreak, munge, verbose,preserveAllSemiColons, disableOptimizations);
		}finally{
			IOUtils.closeWriter(writer);
			IOUtils.closeReader(reader);
		}
	}
	
	/**
	 * css压缩
	 * @param shell
	 * @param src	待压缩文件
	 * @param dest	压缩后输出的目标文件
	 * @throws Exception
	 */
	public void compressorCss(Shell shell,File src,File dest) throws Exception{
		Reader reader = null;
		Writer writer = null;
		try {
			reader = new FileReader(src);
			writer = new FileWriter(dest);
			new CssCompressor(reader).compress(writer, linebreak);
		}finally{
			IOUtils.closeWriter(writer);
			IOUtils.closeReader(reader);
		}
	}
	
	/**
	 * 递归压缩文件夹(文件)
	 * @param shell
	 * @param src
	 * @param suffix
	 * @param type 多个类型可用|隔开，如 JS|CSS
	 * @throws Exception 
	 */
	public void compressorFile(Shell shell,File src,String suffix,int type) throws Exception{
		if(src.isFile()){
			String name = src.getName().toLowerCase();
			String path = src.getAbsolutePath();
			if(name.endsWith(".js") && (JscssDialog.TYPE_JS&type) != 0 && !name.endsWith("."+suffix+".js")){
				compressorJs(shell, src, new File(path = path.substring(0,path.length() - 2) + suffix + ".js"));
			}else if(name.endsWith(".css") && (JscssDialog.TYPE_CSS&type) != 0 && !name.endsWith("."+suffix+".css")){
				compressorCss(shell, src, new File(path = path.substring(0,path.length() - 3) + suffix + ".css"));
			}
		}else if(src.isDirectory()){
			for(File file : src.listFiles()){
				compressorFile(shell, file, suffix, type);
			}
		}
	}
	
}
